﻿using BCCommon;
using BCCommon.Enums;
using BCData.Common.Service.WebsiteServiceItem;
using BCDto.Common.Service.WebsiteServiceItem;
using BCEntity.Common.Service.WebsiteServiceItem;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCData.Common.Service.WebsiteServiceType;
using BCData.Common.Business.WebsiteBusinessCategory;
using BCDto.Common.Service.WebsiteServiceType;
using BCDto.Sim.Admin.Admin;

namespace BCService.Common.Service.WebsiteServiceItem
{
    public class WebsiteServiceItemService : IWebsiteServiceItemService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IWebsiteServiceItemData websiteServiceItemData;
        private readonly IWebsiteServiceTypeData websiteServiceTypeData;
        private readonly IWebsiteBusinessCategoryData websiteBusinessCategoryData;
        private readonly string key = string.Format("{0}", EntityNameConst.WebsiteServiceItemEntity);

        public WebsiteServiceItemService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IWebsiteServiceItemData websiteServiceItemData,
            IWebsiteServiceTypeData websiteServiceTypeData,
            IWebsiteBusinessCategoryData websiteBusinessCategoryData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.websiteServiceItemData = websiteServiceItemData;
            this.websiteServiceTypeData = websiteServiceTypeData;
            this.websiteBusinessCategoryData = websiteBusinessCategoryData;
        }

        #region 检查官网服务项目是否存在
        public bool Exists(int websiteServiceItemId)
        {
            return this.websiteServiceItemData.Exists(websiteServiceItemId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加官网服务项目
        public WebsiteServiceItemDto Add(WebsiteServiceItemRequestDto requestDto,AdminDto adminDto)
        {
            if (!this.websiteServiceTypeData.Exists(requestDto.WebsiteServiceTypeId).Result)
            {
                throw new ArgumentException("此服务类型不存在!");
            }
            if (!this.websiteBusinessCategoryData.Exists(requestDto.WebsiteBusinessCategoryId).Result)
            {
                throw new ArgumentException("此业务分类不存在!");
            }

            var serviceTypeEntity = this.websiteServiceTypeData.GetEntity(requestDto.WebsiteServiceTypeId).Result;
            var businessCategoryEntity = this.websiteBusinessCategoryData.GetEntity(requestDto.WebsiteBusinessCategoryId).Result;
            var entity = requestDto.As<WebsiteServiceItemEntity>();
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;
            entity.WebsiteBusinessCategoryId = businessCategoryEntity.WebsiteBusinessCategoryId;
            entity.Title = businessCategoryEntity.Title;
            entity.WebsiteServiceTypeId = serviceTypeEntity.WebsiteServiceTypeId;
            entity.ServiceTypeName = serviceTypeEntity.ServiceTypeName;
            return this.websiteServiceItemData.Add(entity).GetAwaiter().GetResult().As<WebsiteServiceItemDto>();
        }
        #endregion

        #region 修改官网服务项目
        public WebsiteServiceItemDto Update(WebsiteServiceItemPutDto putDto,AdminDto adminDto)
        {
            if (!this.Exists(putDto.WebsiteServiceItemId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            if (!this.websiteServiceTypeData.Exists(putDto.WebsiteServiceTypeId).Result)
            {
                throw new ArgumentException("此服务类型不存在!");
            }
            if (!this.websiteBusinessCategoryData.Exists(putDto.WebsiteBusinessCategoryId).Result)
            {
                throw new ArgumentException("此业务分类不存在!");
            }
            var serviceTypeEntity = this.websiteServiceTypeData.GetEntity(putDto.WebsiteServiceTypeId).Result;
            var businessCategoryEntity = this.websiteBusinessCategoryData.GetEntity(putDto.WebsiteBusinessCategoryId).Result;
            var entity = putDto.As<WebsiteServiceItemEntity>();
            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            entity.WebsiteBusinessCategoryId = businessCategoryEntity.WebsiteBusinessCategoryId;
            entity.Title = businessCategoryEntity.Title;
            entity.WebsiteServiceTypeId = serviceTypeEntity.WebsiteServiceTypeId;
            entity.ServiceTypeName = serviceTypeEntity.ServiceTypeName;
            var result = this.websiteServiceItemData.Update(entity).GetAwaiter().GetResult().As<WebsiteServiceItemDto>();
            string cacheKey = this.redisService.GetKey(key, entity.WebsiteServiceItemId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 删除官网服务项目
        public bool Delete(int websiteServiceItemId)
        {
            if (!this.Exists(websiteServiceItemId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.websiteServiceItemData.Delete(websiteServiceItemId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteServiceItemId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取官网服务项目
        public WebsiteServiceItemDto Get(int websiteServiceItemId)
        {
            string cacheKey = this.redisService.GetKey(key, websiteServiceItemId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteServiceItemData.GetEntity(websiteServiceItemId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<WebsiteServiceItemDto>();
        }
        #endregion

        #region 获取官网服务项目列表
        public IEnumerable<WebsiteServiceItemDto> GetList(int? websiteServiceTypeId)
        {
            string cacheKey = this.redisService.GetKey(key, websiteServiceTypeId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.websiteServiceItemData.GetList(websiteServiceTypeId).GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<WebsiteServiceItemDto>>();
        }
        #endregion
        #region 获取官网所有服务项目列表
        public IEnumerable<WebsiteServiceViewDto> GetAllList()
        {
            string cacheKey = this.redisService.GetKey(key);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var typeList = this.websiteServiceTypeData.GetList().Result;
                var viewDtos = typeList.As<IEnumerable<WebsiteServiceViewDto>>();
                foreach (var item in viewDtos)
                {
                    item.Items = this.websiteServiceItemData.GetList(item.WebsiteServiceTypeId).Result.As<List<WebsiteServiceItemViewDto>>();
                }
                return viewDtos;
            }, TimeSpan.FromMinutes(30));
            return result;
        }
        #endregion
        #region 获取官网服务项目分页
        public PageableList<WebsiteServiceItemDto> Query(string businessCategoryTitle, string serviceTypeName, string serviceItemName, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, businessCategoryTitle, serviceTypeName, serviceItemName, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteServiceItemData.Query(businessCategoryTitle, serviceTypeName, serviceItemName, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<WebsiteServiceItemDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<WebsiteServiceItemDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion

        #region 启用官网服务项目
        public bool Enabled(int websiteServiceItemId)
        {
            if (!this.Exists(websiteServiceItemId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteServiceItemData.Enabled(websiteServiceItemId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteServiceItemId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 禁用官网服务项目
        public bool Disabled(int websiteServiceItemId)
        {
            if (!this.Exists(websiteServiceItemId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteServiceItemData.Disabled(websiteServiceItemId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteServiceItemId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion
    }
}